package com.smyh.protcol.host;

import com.smyh.UIM.HostFragment;
import com.smyh.UIM.host.param.GPRS_Param;
import com.smyh.UIM.host.param.HardwareParam;
import com.smyh.UIM.host.param.Param_101Activity;
import com.smyh.UIM.host.param.Version_info_Param;
import com.smyh.bean.CmdDataModel;
import com.smyh.bean.GprsParamInfo;
import com.smyh.bean.HostHardwareInfo;
import com.smyh.bean.PointObjInfo;
import com.smyh.bean.Protocol101SetInfo;
import com.smyh.bean.SoftVersionInfo;
import com.smyh.cache.PointValueCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.cache.UIM.host.HostParamInfoMapCache;
import com.smyh.commcache.commd.CommandCache;
import com.smyh.common.MinaClientThread;
import com.smyh.server.ToastShowMsgThread;
import com.smyh.util.AddCrc;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

import java.util.List;
import java.util.Vector;

public class ProtcolStrutsAnaly {
	//TODO 链路地址长度  默认2
	static Integer addrLength = 2;
	//TODO 链路地址  默认0001
	static String linkAddrStr = "0001";
	//TODO 传输原因长度  默认1
	static Integer transCausesLength = 1;
	//TODO 信息体长度 默认2
	static Integer infoObjLength = 2;

	public static void analyMsg(IoSession session,IoBuffer buffer){
		Object obj = session.getAttribute("101_LAST_BUF");
		IoBuffer totalBuf = IoBuffer.allocate(0).setAutoExpand(true);
		/*
		 * 把上次session数据和并到本次数据
		 */
		if(obj != null){
			totalBuf.put((IoBuffer)obj);
			session.removeAttribute("101_LAST_BUF");
		}
		totalBuf.put(buffer).flip();
		/**
		 * 断开连接的开关：数据-固定格式-固定数据:
		 */
		String allStr = totalBuf.getHexDump();
		if(allStr.contains("55 AA 08 D2 09 00 E3 16")){
			MainCahce.on_offLineAllRF();
		}
		System.out.println("ALL:" + allStr);

		for(int i=0;i<totalBuf.limit();i++){
			//101短帧报文最短数据长度：
			int minDataLength = 2 + addrLength + 2;
			
			if( totalBuf.limit() < i + minDataLength ){
				IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
				leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit()-i)).flip();
				session.setAttribute("101_LAST_BUF", leaveBuf);
				System.out.println("存入通道数据!");
				return;
			}
			int head0 = totalBuf.get(i)&0xff;
			int head1 = totalBuf.get(i+1)&0xff;
			int head2 = totalBuf.get(i+2)&0xff;
			int head3 = totalBuf.get(i+3)&0xff;
			
			if(0x10 == head0 && 0x8b == head1){
				int length = 24;
				if(length + i <= totalBuf.limit()){
					//*********进入一个自定义报文过程*************
					byte crcSum = totalBuf.get(i+length-2);
					byte endFix = totalBuf.get(i+length-1);
					if(endFix != Protocol101Struts.FIX_FRAME_END){
						//格式不对，判断继续
						continue;
					}
					
					//校验码是除去 ----报文头----尾----的其他字节和
					byte countCrc = AddCrc.getCrc(totalBuf, i+1, i+length-3);
					if(crcSum == countCrc){

						//得到设备回应
						session.setAttribute("READ_FIRST", 0);
						//报文类型判定：
//						String linkAddr  = totalBuf.duplicate().getSlice(i+2, 2).getHexDump();
						byte[] linkAddrBytes = {totalBuf.get(i+2+1),totalBuf.get(i+2)};
//						linkAddrBytes =  HostCMDUtil.turnBytes(linkAddrBytes);
						String linkAddr = HexStrUtil.byte2HexStr(linkAddrBytes);
						int linkLength = totalBuf.get(i+4)&0xff;
						int transCauseLength = totalBuf.get(i+5)&0xff;
						int vsqLength = totalBuf.get(i+6)&0xff;
						int infoObjLength = totalBuf.get(i+7)&0xff;
						int overTimes = totalBuf.get(i+8)&0xff;
						int reaptTimes = totalBuf.get(i+9)&0xff;
						String hostCode  = totalBuf.duplicate().getSlice(i+10, 2).getHexDump();
						byte[] resverBytes = totalBuf.getSlice(i+12, 10).array();
						Protocol101SetInfo proInfo = new Protocol101SetInfo();
						proInfo.setLinkAddr(linkAddr);
						proInfo.setAddrLength(linkLength);
						proInfo.setTransCauseLength(transCauseLength);
						proInfo.setVsqLength(vsqLength);
						proInfo.setInfoObjLength(infoObjLength);
						proInfo.setOverTimes(overTimes);
						proInfo.setRepeatTimes(reaptTimes);
						proInfo.setHostCode(hostCode);
						proInfo.setResverBytes(resverBytes);
						/**
						 * 更新配置文件数据
						 * 数据地址使用设备的真实地址----
						 */

					//添加上缓存101参数信息
						HostParamInfoMapCache.host101InfoMap.put(MainCahce.chooseDeviceAddr,proInfo);

						Param_101Activity.refreshParam101(proInfo);
						HostCMDUtil.linkAddrStr = linkAddr;
						//**
						//本地参数查询应答
						i += length -1;
					}else{
						System.out.println("crc校验错误!");
					}
				}else{
					byte endFix = totalBuf.get(totalBuf.limit()-1);
					IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
					leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit()-i)).flip();
					session.setAttribute("101_LAST_BUF", leaveBuf);
					System.out.println("报文长度不对,格式长度：" + length + "; 实际报文："+ totalBuf.limit());

					return;
				}
			}
			//===================================================

			if(head0 == 0x68 && head3 == 0x68){
				if(head1 == head2){
					int length = head1;
					if(totalBuf.limit() >= i+4+2+length){

						byte crcSum = totalBuf.get(i+ 4 +2 +length-2);
						byte endFix = totalBuf.get(i+ 4 +2 +length-1);
						if(endFix != Protocol101Struts.FIX_FRAME_END){
							//格式不对，判断继续
							continue;
						}
						//校验码是除去 ----报文头----尾----的其他字节和
						byte countCrc = AddCrc.getCrc(totalBuf, i+4, i+length+3);
						if(crcSum == countCrc){
							IoBuffer fixMockbuf = IoBuffer.allocate(0).setAutoExpand(true);
							fixMockbuf.put(totalBuf).flip();
							byte linkControl = totalBuf.get(i+4);
							int linkSize = addrLength;
							
							byte[] hostLinkAddr = new byte[linkSize];
							for(int jj = 0;jj<linkSize;jj++){
								hostLinkAddr[linkSize -1 -jj] = totalBuf.get(i + 5 + jj);
							}
							
//							{totalBuf.get(i+6),totalBuf.get(i+5)};
							byte dataType = totalBuf.get(i+5 + linkSize);
							byte vsq = totalBuf.get(i+5 + linkSize + 1);
							int transCauseSize = transCausesLength;
							
							byte[] transCause = new byte[transCauseSize];
							for(int jj=0;jj<transCauseSize;jj++){
								
								transCause[transCauseSize -1 -jj] = totalBuf.get( i + 5+linkSize + 2 + jj);
							}
							
							IoBuffer contentBuf = totalBuf.getSlice(i+5 + linkSize + 1 + 1 + transCauseSize + linkSize , 
									length-1-linkSize-1-1-transCauseSize-linkSize);
							
							byte[] infoObjAddr = {contentBuf.get(1),contentBuf.get(0)};
							/**
							 * 数据处理过程有很多非101处理，加入数据是否处理来做依据：
							 */
							boolean dealSign = false;
							
							if(infoObjAddr[0] == Protocol101Struts.protocolParamAddr[0]&&
									infoObjAddr[1] == Protocol101Struts.protocolParamAddr[1]){
								//101协议参数地址
//								String linkAddr = contentBuf.getSlice(0+2,2).getHexDump();
//								byte[] linkAddrBytes = HexStrUtil.hexStr2Bytes(linkAddr);
//								linkAddrBytes =  HostCMDUtil.turnBytes(linkAddrBytes);
								byte[] linkAddrBytes = {contentBuf.get(2),contentBuf.get(2+1)};
								String linkAddr = HexStrUtil.byte2HexStr(linkAddrBytes);
								int linkAddrLength = contentBuf.get(2+2)&0xFF;
								int transCauseLength = contentBuf.get(3+2)&0xFF;
								int vsqLength = contentBuf.get(4+2)&0xFF;
								int infoObjLength = contentBuf.get(5+2)&0xFF;
								int overTimes = contentBuf.get(6+2)&0xFF;
								int repeatTimes = contentBuf.get(7+2)&0xFF;
								String hostCode = contentBuf.getSlice(10,2).getHexDump().replace(" ", "");
								byte[] resverBytes = contentBuf.getSlice(10+2, contentBuf.limit()-12).array();

								Protocol101SetInfo protocolInfoTemp = new Protocol101SetInfo();
								protocolInfoTemp.setLinkAddr(linkAddr);
								protocolInfoTemp.setAddrLength(linkAddrLength);
								protocolInfoTemp.setTransCauseLength(transCauseLength);
								protocolInfoTemp.setVsqLength(vsqLength);
								protocolInfoTemp.setInfoObjLength(infoObjLength);
								protocolInfoTemp.setOverTimes(overTimes);
								protocolInfoTemp.setRepeatTimes(repeatTimes);
								protocolInfoTemp.setResverBytes(resverBytes);
								protocolInfoTemp.setHostCode(hostCode);
								
								/**
								 * 更新配置文件数据
								 * 数据地址使用设备的真实地址----
								 */
								Param_101Activity.refreshParam101(protocolInfoTemp);
								HostCMDUtil.linkAddrStr = linkAddr;
								dealSign = true;
							}
							if(infoObjAddr[0] == Protocol101Struts.hardParamAddr[0]&&
									infoObjAddr[1] == Protocol101Struts.hardParamAddr[1]){
								//硬件参数地址
								int groupNum = contentBuf.get(0+2)&0xFF;
								int batteryWarn = contentBuf.get(1+2)&0xFF;
								int gsmTurnOffVolt = contentBuf.get(2+2)&0xFF;
								int gsmTurnOnVolt = contentBuf.get(3+2)&0xFF;
								int repeatConnects = contentBuf.get(4+2)&0xFF;
								int repeatConnectInter = contentBuf.getShort(5+2)&0xFFFF;
								int mockCycle = contentBuf.getShort(7+2)&0xFFFF;
								int heartCycle = contentBuf.getShort(9+2)&0xFFFF;
								int runStyle = contentBuf.get(11+2)&0xFF;
								byte[] resverBytes = contentBuf.getSlice(12+2, contentBuf.limit()-14).array();
								HostHardwareInfo info = new HostHardwareInfo();
								info.setBatteryWarn(batteryWarn);
								info.setGroupNum(groupNum);
								info.setGsmTurnOffVolt(gsmTurnOffVolt);
								info.setGsmTurnOnVolt(gsmTurnOnVolt);
								info.setHeartCycle(heartCycle);
								info.setMockCycle(mockCycle);
								info.setRepeatConnect(repeatConnects);
								info.setRepeatConnectMinInter(repeatConnectInter);
								info.setResverBytes(resverBytes);
								info.setRunStyle(runStyle);

								HardwareParam.refreshHard(info);
								//Protocol101HostModel.updateHostHardWareInfo(info);
								
								dealSign = true;
							}
							//GPRS参数数据解析
							if(infoObjAddr[0] == Protocol101Struts.grpsParamAddr[0]&&
									infoObjAddr[1] == Protocol101Struts.grpsParamAddr[1]){
								//GPRS参数地址
								String ipA = contentBuf.getSlice(0+2, 4).getHexDump();
								int portA = contentBuf.getShort(4+2)&0xFFFF;
								String ipB = contentBuf.getSlice(6+2, 4).getHexDump();
								int portB = contentBuf.getShort(10+2)&0xFFFF;
								String APN = contentBuf.getSlice(12+2, 20).getHexDump();
								String telNum = contentBuf.getSlice(32+2, 11).getHexDump();
								String telNum2 = contentBuf.getSlice(43+2, 11).getHexDump();
								byte[] resverBytes = contentBuf.getSlice(54+2, 26).array();
								GprsParamInfo info = new GprsParamInfo();
								
								info.setAPN(APN);
								info.setIpA(ipA);
								info.setIpB(ipB);
								info.setPortA(portA);
								info.setPortB(portB);
								info.setResverBytes(resverBytes);
								info.setTelNum(telNum);
								info.setTelNum2(telNum2);

								GPRS_Param.refreshGPRS(info);
								dealSign = true;
							}
							//软件版本信息数据解析
							if(infoObjAddr[0] == Protocol101Struts.softVersionParamAdrr[0]&&
									infoObjAddr[1] == Protocol101Struts.softVersionParamAdrr[1]){
								//GPRS参数地址
								Integer[] softVersionArray = new Integer[3];
								softVersionArray[0] = contentBuf.get(2+0)&0xff;
								softVersionArray[1] = contentBuf.get(2+1)&0xff;
								softVersionArray[2] = contentBuf.get(2+2)&0xff;
								
								Integer[] ipAddrForCardArray = new Integer[4];
								ipAddrForCardArray[0] = contentBuf.get(2+3)&0xff;
								ipAddrForCardArray[1] = contentBuf.get(2+3+1)&0xff;
								ipAddrForCardArray[2] = contentBuf.get(2+3+2)&0xff;
								ipAddrForCardArray[3] = contentBuf.get(2+3+3)&0xff;
								
								int singalGrade = contentBuf.get(2+7)&0xff;
								
								byte[] resverBytes = contentBuf.getSlice(2+8, 52).array();
								SoftVersionInfo info = new SoftVersionInfo();
								
								info.setSoftVersionArray(softVersionArray);
								info.setIpAddrForCardArray(ipAddrForCardArray);
								info.setSingalGrade(singalGrade);
								info.setResverBytes(resverBytes);

								Version_info_Param.refreshVersion(info);
								dealSign = true;
							}
							
							if(infoObjAddr[0] == Protocol101Struts.pointDigit[0]){
								//第几帧遥信点位：
								byte digitB = Protocol101Struts.pointDigit[1];
								int numberFrame = 0;
								if(infoObjAddr[1] == digitB){
									if(transCause[0] == 0x13){
										PointValueCache.pointDigitalMap.clear();
									}
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (digitB + 1)){
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (digitB + 2)){
									numberFrame = 2;
								}
								/**
								 * 读取遥信数据成功后，回复数据
								 */
							
								if(numberFrame > 0){
									if(transCause[0] == 0x11){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										//TODO
										CmdDataModel cmdModel = CommandCache.commandMap.get(CommandCache.POINT_WRITE_CMD);
										if(cmdModel!=null){
											if(cmdModel.getContentsList().size()>0){
												cmdModel.getContentsList().remove(0);
												cmdModel.setCounts((byte) 0);
											}
										}

										i += 4+2+length -1;
										continue;
									}
									
									if(transCause[0] == 0x13){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("读取成功！")).start();
										IoBuffer sureBuf = HostCMDUtil.surePointWriteOrReadMsg(transCause[0], infoObjAddr);
										//TODO
										IoSession session01 = MinaClientThread.session;
										if(session01 != null){
											session01.write(sureBuf);
										}

									}
								}
								
								int counts = vsq&0x7f;
								List<PointObjInfo> listObj = new Vector<PointObjInfo>();
								try {
									for(int j=0;j<counts;j++){
										Integer pointV = (contentBuf.get(i+2+j*4)&0xff) + ((contentBuf.get(i+2+j*4+1)&0xff)*256);
										Integer groupNum = contentBuf.get(i+2+j*4+2)&0xff;
										Integer dataPoint = contentBuf.get(i+2+j*4+3)&0xff;
										//TODO
										PointObjInfo objTemp = new PointObjInfo();
										objTemp.setPointV(pointV);
										objTemp.setGroupNum(groupNum);
										objTemp.setDataPoint(dataPoint);
										
										listObj.add(objTemp);
									}
								} catch (IndexOutOfBoundsException e) {
								}
								
								PointValueCache.updatePointDigitalMap(listObj);
								dealSign = true;
							}

							if(infoObjAddr[0] == Protocol101Struts.pointMock[0]){
								//第几帧遥信点位：
								int numberFrame = 0;
								if(infoObjAddr[1] == Protocol101Struts.pointMock[1]){
									if(transCause[0] == 0x13){
										//TODO
										PointValueCache.pointMockMap.clear();
									}
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (Protocol101Struts.pointMock[1] + 1)){
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (Protocol101Struts.pointMock[1] + 2)){
									numberFrame = 2;
								}
								/**
								 * 读取遥信数据成功后，回复数据
								 */
								if(numberFrame > 0){
									if(transCause[0] == 0x11){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										//TODO
										CmdDataModel cmdModel = CommandCache.commandMap.get(CommandCache.POINT_WRITE_CMD);
										if(cmdModel!=null){
											if(cmdModel.getContentsList().size()>0){
												cmdModel.getContentsList().remove(0);
												cmdModel.setCounts((byte) 0);
											}
										}

										i += 4+2+length -1;
										continue;
									}
									
									if(transCause[0] == 0x13){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										IoBuffer sureBuf = HostCMDUtil.surePointWriteOrReadMsg(transCause[0], infoObjAddr);
										//TODO
										IoSession session01 = MinaClientThread.session;
										if(session01 != null){
											session01.write(sureBuf);
										}
									}
									
								}
								
								int counts = vsq&0x7f;
								//TODO
								List<PointObjInfo> listObj = new Vector<PointObjInfo>();
								for(int j=0;j<counts;j++){
									Integer pointV = (contentBuf.get(i+2+j*4)&0xff) + (contentBuf.get(i+2+j*4+1)&0xff)*256;
									Integer groupNum = contentBuf.get(i+2+j*4+2)&0xff;
									Integer dataPoint = contentBuf.get(i+2+j*4+3)&0xff;
									
									PointObjInfo objTemp = new PointObjInfo();
									objTemp.setPointV(pointV);
									objTemp.setGroupNum(groupNum);
									objTemp.setDataPoint(dataPoint);
									
									listObj.add(objTemp);
								}
								
								PointValueCache.updatePointMockMap(listObj);
								dealSign = true;
							}
							
							if(infoObjAddr[0] == Protocol101Struts.pointSet[0]){
								//第几帧遥信点位：
								int numberFrame = 0;
								if(infoObjAddr[1] == Protocol101Struts.pointSet[1]){
									if(transCause[0] == 0x13){
										//TODO
										PointValueCache.pointSetMap.clear();
									}
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (Protocol101Struts.pointSet[1] + 1)){
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (Protocol101Struts.pointSet[1] + 2)){
									numberFrame = 2;
								}
								/**
								 * 读取遥信数据成功后，回复数据
								 */
								if(numberFrame > 0){
									if(transCause[0] == 0x11){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										//TODO
										CmdDataModel cmdModel = CommandCache.commandMap.get(CommandCache.POINT_WRITE_CMD);
										if(cmdModel!=null){
											if(cmdModel.getContentsList().size()>0){
												cmdModel.getContentsList().remove(0);
												cmdModel.setCounts((byte) 0);
											}
										}

										i += 4+2+length -1;
										continue;
									}
									
									if(transCause[0] == 0x13){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										IoBuffer sureBuf = HostCMDUtil.surePointWriteOrReadMsg(transCause[0], infoObjAddr);
										//TODO
										IoSession session01 = MinaClientThread.session;
										if(session01 != null){
											session01.write(sureBuf);
										}
									}
									
								}
								
								int counts = vsq&0x7f;
								//TODO
								List<PointObjInfo> listObj = new Vector<PointObjInfo>();
								for(int j=0;j<counts;j++){
									Integer pointV = (contentBuf.get(i+2+j*4)&0xff) + (contentBuf.get(i+2+j*4+1)&0xff)*256;
									Integer groupNum = contentBuf.get(i+2+j*4+2)&0xff;
									Integer dataPoint = contentBuf.get(i+2+j*4+3)&0xff;
									
									PointObjInfo objTemp = new PointObjInfo();
									objTemp.setPointV(pointV);
									objTemp.setGroupNum(groupNum);
									objTemp.setDataPoint(dataPoint);
									
									listObj.add(objTemp);
								}
								
								PointValueCache.updatePointSetMap(listObj);
								dealSign = true;
							}
							
							if(infoObjAddr[0] == Protocol101Struts.pointControl[0]){
								//第几帧遥信点位：
								int numberFrame = 0;
								if(infoObjAddr[1] == Protocol101Struts.pointControl[1]){
									if(transCause[0] == 0x13){
										//TODO
										PointValueCache.pointControlMap.clear();
									}
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (Protocol101Struts.pointControl[1] + 1)){
									numberFrame = 1;
								}
								if(infoObjAddr[1] == (Protocol101Struts.pointControl[1] + 2)){
									numberFrame = 2;
								}
								/**
								 * 读取遥信数据成功后，回复数据
								 */
								if(numberFrame > 0){
									if(transCause[0] == 0x11){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										//TODO
										CmdDataModel cmdModel = CommandCache.commandMap.get(CommandCache.POINT_WRITE_CMD);
										if(cmdModel!=null){
											if(cmdModel.getContentsList().size()>0){
												cmdModel.getContentsList().remove(0);
												cmdModel.setCounts((byte) 0);
											}
										}

										i += 4+2+length -1;
										continue;
									}
									
									if(transCause[0] == 0x13){
//										UIElement.showMessageDialog( "修改成功");
										new Thread(new ToastShowMsgThread("修改成功！")).start();
										IoBuffer sureBuf = HostCMDUtil.surePointWriteOrReadMsg(transCause[0], infoObjAddr);
										//TODO
										IoSession session01 = MinaClientThread.session;
										if(session01 != null){
											session01.write(sureBuf);
										}
									}
									
								}
								
								int counts = vsq&0x7f;
								//TODO
								List<PointObjInfo> listObj = new Vector<PointObjInfo>();
								for(int j=0;j<counts;j++){
									Integer pointV = (contentBuf.get(i+2+j*4)&0xff) + (contentBuf.get(i+2+j*4+1)&0xff)*256;
									Integer groupNum = contentBuf.get(i+2+j*4+2)&0xff;
									Integer dataPoint = contentBuf.get(i+2+j*4+3)&0xff;
									
									PointObjInfo objTemp = new PointObjInfo();
									objTemp.setPointV(pointV);
									objTemp.setGroupNum(groupNum);
									objTemp.setDataPoint(dataPoint);
									
									listObj.add(objTemp);
								}
								
								PointValueCache.updatePointControlMap(listObj);
								dealSign = true;
							}
							/**
							 * 如果数据没有进入上面的方法》进入下面判断逻辑
							 */
							if( ! dealSign ){
								switch (dataType) {
								case 1:{
									try {
										if(transCause[0] == 0x14){
											//单点遥信，总召
											//AllCallJDialogCache.updateReceiveDigitalTime();
											//ProtocolBusyDataProgress.dealSingleDigital(hostLinkAddr, vsq, contentBuf);
											return;
										}else{
											ProtocolBusyDataProgress.dealSingleDigital(hostLinkAddr, vsq, contentBuf);
										}
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
									break;
								}
								case 0x1E:{
									switch (transCause[0]) {
									case 0x07:
										//TODO
										if(MinaClientThread.session!=null&&MinaClientThread.session.isConnected()){
											IoBuffer hisDigitalCMD = HostCMDUtil.sureMsgSuc();
											MinaClientThread.session.write(hisDigitalCMD);
										}
										break;
									case 0x05:
										//ProtocolBusyDataProgress.dealSingleDigitalTime(hostLinkAddr, vsq, contentBuf);
										break;

									default:
										try {
											//ProtocolBusyDataProgress.dealSingleDigitalTimeNoneHistory(hostLinkAddr, vsq, contentBuf);
										} catch (Exception e) {
										}
										break;
									}
								}
								case 3:{
									if(transCause[0] == 0x14){
										//双点遥信，总召
										//TODO
									//	AllCallJDialogCache.updateReceiveDigitalTime();
										return;
									}
									break;
								}case 9:{
									if(transCause[0] == 0x14){
										//标一化遥测，总召
										//TODO
										//AllCallJDialogCache.updateReceiveMockTime();
										return;
									}
									break;
								}
								case 0xb:{
									try {
										if(transCause[0] == 0x14){
											//标度化遥测，总召
											//TODO
											//AllCallJDialogCache.updateReceiveMockTime();
											//ProtocolBusyDataProgress.dealStandMock(hostLinkAddr, vsq, contentBuf);
											return;
										}else if(transCause[0] == 0x5a){
											//标度化遥测，主机召唤
											
											ProtocolBusyDataProgress.dealHostStandMock(hostLinkAddr, vsq, contentBuf);
											return;
										}else{
											ProtocolBusyDataProgress.dealStandMock(hostLinkAddr, vsq, contentBuf);
										}
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
									break;
								}
								case 0x23:{
									//定点遥测
									switch (transCause[0]) {
									case 0x07:
										//激活确认
										IoBuffer sureMsgSuc = HostCMDUtil.sureMsgSuc();
										//TODO
										MinaClientThread.session.write(sureMsgSuc);
										return;
									case 0x05:
										//ProtocolBusyDataProgress.fixedMock(hostLinkAddr, vsq,fixMockbuf);
										break;
									default:
										//ProtocolBusyDataProgress.dealStandMockTime(hostLinkAddr, vsq, contentBuf);
										break;
									}
									
									break;
								}
									case 0x64:{
										//总召
										if(transCause[0] == 0x7){
											//总召：21总召确认
											//TODO
											//AllCallJDialogCache.updateActiveSureTime();
										}else if(transCause[0] == 0xA){
											//总召：22总召结束
											//TODO
										/*HostProgressState.STATE_WORD = 10002;
										AllCallJDialogCache.updateOverCallTime();*/
										}
										return;
									}

									case 0x67: {
										//对时报文回复
										switch (transCause[0]) {
											case 0x4: {
												CmdDataModel cmdModel = CommandCache.commandMap.get(CommandCache.HOST_SYNC_CMD);
												if (cmdModel != null) {
													if (cmdModel.getContentsList().size() > 0) {
														cmdModel.getContentsList().remove(0);
														cmdModel.setCounts((byte) 0);
													}
												}
//												ContentMainUI.lastInstance.setStatusLine("同步成功！");
												new Thread(new ToastShowMsgThread("同步成功！")).start();
												break;
											}

											case 0x7: {
												//时钟同步命令(激活确认)
												IoBuffer confirm = HostCMDUtil.synchTimeConfirm();
												session.write(confirm);
												//对时完成
//												HostProgressState.STATE_WORD = 10003;
//												ContentMainUI.lastInstance.setStatusLine("对时成功！");
												new Thread(new ToastShowMsgThread("对时成功！")).start();

												break;

											}

										}
									}

									default:
									break;
								}
							}
							i += 4+2+length -1;
						}else{
							System.out.println("crc校验错误!");
						}
						
					}else{
						IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
						leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit()-i)).flip();
						session.setAttribute("101_LAST_BUF", leaveBuf);
						System.out.println("报文长度不对,格式长度：" + (length-i-2-4) + "; 实际报文："+ totalBuf.limit());
						
						return;
					}
				}else{
					System.out.println("报文格式不正确应 如:（68 FF FF 68");
				}
			}
		}
	}
}
